package com.studentgrowth.controller.home;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;

import com.studentgrowth.model.course.Course;
import com.studentgrowth.repository.CourseRepository;
import org.springframework.beans.factory.annotation.Autowired;
import com.studentgrowth.model.Category;
import com.studentgrowth.repository.CategoryRepository;

@RestController
@RequestMapping("/api/home")
public class HomeController {

    private static final Logger log = LoggerFactory.getLogger(HomeController.class);
    @Autowired
    private CourseRepository courseRepository;

    @Autowired
    private CategoryRepository categoryRepository;

    // 1. 轮播图接口
    @GetMapping("/banners")
    public List<String> getBanners() {
        return Arrays.asList(
            "https://example.com/banner1.jpg",
            "https://example.com/banner2.jpg",
            "https://example.com/banner3.jpg"
        );
    }

    // 2. 课程分类接口
    @GetMapping("/categories")
    public List<Map<String, String>> getCategories() {
        List<Map<String, String>> categories = new ArrayList<>();
        categories.add(createCategory("algo", "算法"));
        categories.add(createCategory("python", "Python"));
        categories.add(createCategory("bigdata", "大数据"));
        categories.add(createCategory("ml", "机器学习"));
        categories.add(createCategory("ml", "数据结构"));
        return categories;
    }

    private Map<String, String> createCategory(String code, String name) {
        Map<String, String> map = new HashMap<>();
        map.put("code", code);
        map.put("name", name);
        return map;
    }

    // 3. 推荐课程接口
    @GetMapping("/recommend-courses")
    public List<Map<String, Object>> getRecommendCourses(HttpServletRequest request) {
        String baseUrl = getBaseUrl(request);
        List<Course> courses = courseRepository.findRecommendCourses();
        List<Map<String, Object>> result = new ArrayList<>();
        for (Course c : courses) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", c.getId());
            map.put("name", c.getName());
            map.put("cover", baseUrl + c.getCover());
            map.put("desc", c.getDescription());
            result.add(map);
        }
        return result;
    }

    private String getBaseUrl(HttpServletRequest request) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort();
    }

    // 4. 热门分类接口
    @GetMapping("/hot-categories")
    public List<Map<String, Object>> getHotCategories(HttpServletRequest request) {
        String baseUrl = getBaseUrl(request);
        List<Category> categories = categoryRepository.findHotCategories();
        List<Map<String, Object>> result = new ArrayList<>();
        for (Category c : categories) {
            Map<String, Object> map = new HashMap<>();
            map.put("id", c.getId());
            map.put("name", c.getName());
            map.put("cover", baseUrl + c.getCover());
            map.put("courseCount", c.getCourseCount());
            result.add(map);
        }
        return result;
    }
} 